package org.tema.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlInputHidden;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.persistence.NoResultException;

import org.tema.ejb.ProductBeanLocal;
import org.tema.entities.Product;


public class ManageProductsBean {
	
	// navigation constants
	private static final String PRODUCTS_LIST = "productsList";
	private static final String EDIT_PRODUCT = "editProduct";

	@EJB
	private ProductBeanLocal productBean;
	private String name;
	private String version;
	private String description;
	private  ProductSearchTypes searchDropDown;
	private String searchWord;
	private Collection <Product> productList = null;
	private int productId;
	private Product singleProduct;
	private HtmlDataTable dataTable;
		//private User dataItem = new User();
	private HtmlInputHidden dataItemId = new HtmlInputHidden();
	
	public ManageProductsBean(){}

	public HtmlDataTable getDataTable() {
		return dataTable;
	}

	public void setDataTable(HtmlDataTable dataTable) {
		this.dataTable = dataTable;
	}

	public HtmlInputHidden getDataItemId() {
		return dataItemId;
	}

	public void setDataItemId(HtmlInputHidden dataItemId) {
		this.dataItemId = dataItemId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	///real methods besides accessors
	
	
	public ProductSearchTypes getSearchDropDown() {
		return searchDropDown;
	}

	public void setSearchDropDown(ProductSearchTypes searchDropDown) {
		this.searchDropDown = searchDropDown;
	}

	public String getSearchWord() {
		return searchWord;
	}

	public void setSearchWord(String searchWord) {
		this.searchWord = searchWord;
	}

	public Collection<Product> getProductList() {
		if(productList == null ){
			this.findAll();
		}
		return productList;
	}

	public void setProductList(Collection<Product> productList) {
		
		this.productList = productList;
	}

	public int getProductId() {
		return productId;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public Product getSingleProduct() {
		return singleProduct;
	}

	public void setSingleProduct(Product singleProduct) {
		this.singleProduct = singleProduct;
	}

	public String createProduct() {
	   	FacesContext facesContext = FacesContext.getCurrentInstance(); 
    	String creatorName = facesContext.getExternalContext().getRemoteUser();
		productBean.addProduct(getName(), getDescription(), getVersion(), creatorName);
		// add status message
		Status.addInfo("Product " + this.name +" created. ");
		// update products list
		findAll();
		return PRODUCTS_LIST; 
	}
	
	public String deleteProduct() {
		singleProduct = (Product) dataTable.getRowData();
		productBean.deleteProduct(singleProduct);
		// add status message
		Status.addInfo("Product " + singleProduct.getName() + " deleted. ");
		// update products list
		findAll();
		return PRODUCTS_LIST;
	}
	
	public String displayProduct() {
		Product product1 = (Product) dataTable.getRowData();
		this.setSingleProduct ( productBean.findProductById(product1.getId()) );
		return EDIT_PRODUCT;
	}
	
	
	public String editProduct() {
    	FacesContext facesContext = FacesContext.getCurrentInstance(); 
    	String modifierName = facesContext.getExternalContext().getRemoteUser();
		productBean.updateProduct(singleProduct, modifierName);
		// add status message
		Status.addInfo("Product " + singleProduct.getName() +" modified. ");
		// update products list
		findAll();
		return PRODUCTS_LIST;
	}
	
	
	public String  selectSearch(){
		String result;
		switch (this.getSearchDropDown()) {
        case ID:  result = findById( this.getSearchWord()); break;
        case NAME:  result = findByName(this.getSearchWord()); break;
        case VERSION:  result = findByVersion(this.getSearchWord()); break;
        case ALLPRODUCTS:  result = findAll(); break;
        default: result = findAll();
		}
		return result;
	}
	
	////
	public String  findAll(){
		//String result;
		setProductList( ( Collection <Product> ) productBean.findAllProducts());
		if ( getProductList().isEmpty() ){
			Status.addInfo("No products available.");
		}
		return PRODUCTS_LIST;
	}
	
	public String  findById(String searchWord){	
		try {
			int id =  Integer.parseInt(searchWord);
			setSingleProduct( productBean.findProductById( id ));
			this.setProductList(Arrays.asList(singleProduct));	
			if (  getProductList().isEmpty() ){
				Status.addInfo("No products available with id: " + id );
			}
		}
		catch(EJBException e){
			Status.addError(e.getCause().getMessage());
		}
		catch(NumberFormatException nf){
			Status.addError(nf.getCause().getMessage());
		}
		catch(NullPointerException np){
			Status.addError(np.getCause().getMessage());
		}
		return PRODUCTS_LIST;
	}
	
	public String findByName( String searchWord){
		try{
			setProductList( ( Collection <Product> ) productBean.findProductsByName(searchWord));
			if (  getProductList().isEmpty() ){
				Status.addInfo("No products available with name: " + searchWord );
			}
		}
		catch(EJBException e){
			Status.addError(e.getCause().getMessage());
		}
		return PRODUCTS_LIST;
	}
	
	public String findByVersion( String searchWord){
		try{
			setProductList( ( Collection <Product> ) productBean.findProductByVersion(searchWord));
			if (  getProductList().isEmpty() ){
				Status.addInfo("No products available with version: " + searchWord );
			}
		}
		catch(EJBException e){
			Status.addError(e.getCause().getMessage());
		}
		return PRODUCTS_LIST;
	}
	
    public Collection<SelectItem> getItems() {
    	ProductSearchTypes[] values = ProductSearchTypes.values();
        Collection<SelectItem> list = new ArrayList<SelectItem>(values.length);
        for (ProductSearchTypes value : values) {            
        	list.add(new SelectItem(value, value.getLabel()));           
        } 
        return list;
    }
    
}
